package com.scs.application.modules.wm.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.core.utils.DateUtils;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.dto.ExpandColumnDTO;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.wm.dto.OprtPrepareDTO;
import com.scs.application.modules.wm.entity.OprtPrepareItem;
import com.scs.application.modules.wm.service.OprtPrepareItemService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/wm/oprt_prepare_item")
public class OprtPrepareItemController extends CrudController<OprtPrepareItemService, OprtPrepareItem>{
    /**
     * 手术备货扫码添加明细
     * 扫码锁库
     * @param oprtPrepareDTO
     * @return
     */
    @PostMapping("/addPrepareItem")
    public RestResult addPrepareItem(@RequestBody @Valid OprtPrepareDTO oprtPrepareDTO) {
        service.addPrepareItem(oprtPrepareDTO.getPrepareId(), oprtPrepareDTO.getUniqueCode(), oprtPrepareDTO.getWarehouseId());
        return RestResult.ok("数据保存成功");
    }


    /**
     * 批量添加
     **/
    @PostMapping("/addBatch")
    synchronized public RestResult addBatch(@RequestBody @Valid List<OprtPrepareItem> itemList) {
        service.addBatch(itemList);
        return RestResult.ok();
    }
    /**
     * 扫描添加
     **/
    @PostMapping("/addScan")
    synchronized public RestResult addBatch(@RequestBody @Valid OprtPrepareDTO item) {
        service.addScan(item);
        return RestResult.ok();
    }

    @PostMapping("/addByOprtApply")
    public RestResult addByOprtApply(@RequestBody Map<String,Serializable> map){
        service.addByOprtApply(map);
        return RestResult.ok("数据保存成功");
    }

    /**
     * 删除跟台包备货明细
     * 并释放库存（解锁）
     * @param id
     * @return
     */
    @DeleteMapping("/deletePackage")
    public RestResult deletePackage(@RequestParam("id") Serializable id){
        service.deletePackage(id);
        return RestResult.ok("删除成功");
    }

    /**
     * 删除包内耗材
     * 并释放库存（解锁）
     * @param id
     * @return
     */
    @DeleteMapping("/deleteMatr")
    public RestResult deleteMatr(@RequestParam("id") Serializable id){
        service.deleteMatr(id);
        return RestResult.ok("删除成功");
    }

    @Override
    @GetMapping("/page")
    public PageResult page() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Page<OprtPrepareItem> page = (Page<OprtPrepareItem>) service.page(queryContext);
        this.fillDataPage(page);
        return PageResult.ok("查询成功", page.getRecords() , page.getTotal());
    }

    private void fillDataPage (Page<OprtPrepareItem> page ) {
        if (page.getRecords() == null || page.getRecords().size() == 0) {
            return;
        }
        List<OprtPrepareItem> itemList = page.getRecords();

        // 包
        List<OprtPrepareItem> itemPackList = itemList.stream().filter(item -> StringUtils.isNotBlank(item.getPackId())).collect(Collectors.toList());
        if (ObjectUtil.length(itemPackList) == 0) {
            return;
        }

        List<String> packItemIdLIst = itemPackList.stream().map(OprtPrepareItem::getId).collect(Collectors.toList());

        // 包明细
        Map<String,List<OprtPrepareItem>> itemChildList = service.list(Wrappers.<OprtPrepareItem>query().isNotNull("pack_prepare_item_id").in("pack_prepare_item_id", packItemIdLIst))
                .stream()
                .collect(Collectors.groupingBy(OprtPrepareItem::getPackPrepareItemId));

        List<ExpandColumnDTO> childCols;
        ExpandColumnDTO expandColumnDTO;

        for(OprtPrepareItem packItem : itemPackList){
            packItem.setShowExpand(true);
            packItem.setChildData(itemChildList.get(packItem.getId()));

            childCols = new ArrayList<>();
            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("matrName").setTitle("商品名称").setColumnWidth(220);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("matrSpec").setTitle("商品规格").setColumnWidth(100);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("sn").setTitle("SN").setColumnWidth(150);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("rfid").setTitle("RFID").setColumnWidth(150);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("udi").setTitle("UDI").setColumnWidth(150);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("lot").setTitle("批次").setColumnWidth(150);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("expDate").setTitle("效期").setColumnWidth(120).setDatePattern(DateUtils.DATE_PATTERN);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("mfgDate").setTitle("生产日期").setColumnWidth(120).setDatePattern(DateUtils.DATE_PATTERN);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("mfgName").setTitle("厂商").setColumnWidth(120).setDatePattern(DateUtils.DATE_PATTERN);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("packageUnit").setTitle("包装单位").setColumnWidth(80);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("packageQty").setTitle("包装数量").setColumnWidth(80);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("amount").setTitle("金额").setColumnWidth(80);
            childCols.add(expandColumnDTO);
            packItem.setChildCols(childCols);
        }
    }

}
